import request from '@/utils/request.js'
//判断是否为空
const isFalsy = (value) => value === 0 ? false : !value

//去除为空的参数
const result = (data) => {
  const result = {
    ...data
  }
  Object.keys(result).forEach(key => {
    const value = result[key]
    if (isFalsy(value)) {
      delete result[key]
    }
  })
  return result
}
//获取内容信息
export const getArticlesInfo = (data) => {
  //去掉空值参数
  const params = result(data)
  return request({
    method: 'GET',
    url: 'mp/v1_0/articles',
    params
  })
}


//获取频道信息
export const getAllChannelsInfo = () => {
  return request({
    method: 'GET',
    url: 'mp/v1_0/channels'
  })
}

//删除文章
export const deleteArticleInfo = (id) => {
  return request({
    method: 'DELETE',
    url: 'mp/v1_0/articles/' + id
  })
}


//发表文章
export const publishArticleInfo = (data, draft) => {
  return request({
    method: 'POST',
    url: 'mp/v1_0/articles',
    params: {
      draft
    },
    data: data
  })
}

//获取指定信息
export const getArticleInfo = (id) => {
  return request({
    method: 'GET',
    url: 'mp/v1_0/articles/' + id
  })
}

//编辑文章
export const updateArticleInfo = (id, draft, data) => {
  return request({
    method: 'PUT',
    url: 'mp/v1_0/articles/' + id,
    params: {
      draft
    },
    data
  })
}
